require("Script.Common.Debug")
require("Script.GameConfigs.GlobalConfigs")

local CardControl = {
	--CardType = ECardType.Queen;
	--��CardControl�б�����Ե���ĵط���ÿ��CanMoveTo�����������ֵ���û��������жԱ�
	canAchieveTiles = {nil,nil,nil,nil,nil,nil};
};
function CardControl:ReceiveBegin( ... )
	-- body
end

function CardControl:PlayerChoseTile(tile)
	print("CardControl:PlayerChoseTile")
	local dir
	local targetTile
	for k,v in pairs(self.canAchieveTiles) do
		if v==tile then 
			dir = k 
			targetTile = v 
			break;
		end
	end
	return dir,targetTile
end

function CardControl:MoveLeftOneStep (tile)
	local dir
	local targetTile
	if(tile.Left~=nil) then 
		dir = EDirection.Left
		targetTile = tile.Left;
	end
	return dir,targetTile
end

function CardControl:QueenCanMoveTo(tile)
	-- Queen ���ⷽ��ǰ��ֱ��ǰ������������/����ң���Ҫ�ж���
	-- ����ÿ������ֱ��ǰ������������/����ң���ÿ��������������ĸ������canAchieveTiles������
	self.canAchieveTiles = {};

	local temp = tile
	Debug:Log("CardControl:QueenCanMoveTo", KismetSystemLibrary.GetDisplayName(tile))

	--Debug:Log("FirstLeftUp", temp.LeftUp, temp.LeftUp.isDestroyed, temp.LeftUp:GetPlayerOnThisTile())
	-- LeftUp
	while(temp.LeftUp~=nil and temp.LeftUp.isDestroyed==false and temp.LeftUp:GetPlayerOnThisTile()==nil)
	do
		temp = temp.LeftUp;
		--Debug:Log("CardControl:QueenCanMoveToLeftUp",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.LeftUp]=0;
		Debug:Log("CardControl:QueenCanMoveTo LeftUp is nil")
	else 
		self.canAchieveTiles[EDirection.LeftUp]=temp;
	end

	temp = tile
	-- Left
	while(temp.Left~=nil and temp.Left.isDestroyed==false and temp.Left:GetPlayerOnThisTile()==nil)
	do
		temp = temp.Left;
		--Debug:Log("CardControl:QueenCanMoveToLeft",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.Left]=0;
		Debug:Log("CardControl:QueenCanMoveTo Left is nil")
	else 
		self.canAchieveTiles[EDirection.Left]=temp;
	end

	temp = tile
	-- LeftDown
	while(temp.LeftDown~=nil and temp.LeftDown.isDestroyed==false and temp.LeftDown:GetPlayerOnThisTile()==nil)
	do
		temp = temp.LeftDown;
		--Debug:Log("CardControl:QueenCanMoveToLeftDown",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.LeftDown]=0;
		Debug:Log("CardControl:QueenCanMoveTo LeftDown is nil")
	else 
		self.canAchieveTiles[EDirection.LeftDown]=temp;
	end

	temp=tile
	-- RightUp
	while(temp.RightUp~=nil and temp.RightUp.isDestroyed==false and temp.RightUp:GetPlayerOnThisTile()==nil)
	do
		temp = temp.RightUp;
		--Debug:Log("CardControl:QueenCanMoveToRightUp",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.RightUp]=0;
		Debug:Log("CardControl:QueenCanMoveTo RightUp is nil")
	else 
		self.canAchieveTiles[EDirection.RightUp]=temp;
	end

	temp=tile
	-- Right
	while(temp.Right~=nil and temp.Right.isDestroyed==false and temp.Right:GetPlayerOnThisTile()==nil)
	do
		temp = temp.Right;
		--Debug:Log("CardControl:QueenCanMoveToRight",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.Right]=0;
		Debug:Log("CardControl:QueenCanMoveTo RightUp is nil")
	else 
		self.canAchieveTiles[EDirection.Right]=temp;
	end

	temp=tile
	-- RightDown
	while(temp.RightDown~=nil and temp.RightDown.isDestroyed==false and temp.RightDown:GetPlayerOnThisTile()==nil)
	do
		temp = temp.RightDown;
		--Debug:Log("CardControl:QueenCanMoveToRightDown",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.RightDown]=0;
		Debug:Log("CardControl:QueenCanMoveTo RightDown is nil")
	else 
		self.canAchieveTiles[EDirection.RightDown]=temp;
	end

	Debug:Log("CardControl:QueenCanMoveTo", self.canAchieveTiles)
	return self.canAchieveTiles;
end

function CardControl:HengCarCanMoveTo(tile)
	-- �ᳵ������/�ҷ���ǰ��ֱ��ǰ�����������ѣ���Ҫ�ж���
	-- ������/�ҷ���ֱ��ǰ�����������ѣ���ÿ��������������ĸ������canAchieveTiles������
	Debug:Log("CardControl:HengCarCanMoveTo")
	self.canAchieveTiles = {};
	local temp = tile
	-- LeftUp
	self.canAchieveTiles[EDirection.LeftUp] = 0 
	-- Left
	while(temp.Left~=nil and temp.Left.isDestroyed==false)
	do
		temp = temp.Left;
		--Debug:Log("CardControl:HengCarCanMoveToLeft",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.Left]=0;
		Debug:Log("CardControl:HengCarCanMoveTo Left is nil")
	else 
		self.canAchieveTiles[EDirection.Left]=temp;
	end
	-- LeftDown
	self.canAchieveTiles[EDirection.LeftDown] = 0

	temp=tile
	-- RightUp
	self.canAchieveTiles[EDirection.RightUp] = 0
	-- Right
	while(temp.Right~=nil and temp.Right.isDestroyed==false)
	do
		temp = temp.Right;
		--Debug:Log("CardControl:HengCarCanMoveToRight",KismetSystemLibrary.GetDisplayName(temp))
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.Right]=0;
		Debug:Log("CardControl:HengCarCanMoveTo Right is nil")
	else 
		self.canAchieveTiles[EDirection.Right]=temp;
	end
	-- RightDown
	self.canAchieveTiles[EDirection.RightDown] = 0

	return self.canAchieveTiles;
end

function CardControl:PieCarCanMoveTo(tile)
	-- Ʋ����������/���·���ǰ��ֱ��ǰ�����������ѣ���Ҫ�ж���
	-- ��������/���·���ֱ��ǰ�����������ѣ���ÿ��������������ĸ������canAchieveTiles������
	self.canAchieveTiles = {};
	local temp = tile
	-- LeftUp
	while(temp.LeftUp~=nil and temp.LeftUp.isDestroyed==false)
	do
		temp = temp.LeftUp;
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.LeftUp]=0;
	else 
		self.canAchieveTiles[EDirection.LeftUp]=temp;
	end
	-- Left
	self.canAchieveTiles[EDirection.Left] = 0
	-- LeftDown
	self.canAchieveTiles[EDirection.LeftDown] = 0

	temp=tile
	-- RightUp
	self.canAchieveTiles[EDirection.RightUp] = 0
	-- Right
	self.canAchieveTiles[EDirection.Right] = 0
	-- RightDown
	while(temp.RightDown~=nil and temp.RightDown.isDestroyed==false)
	do
		temp = temp.RightDown;
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.RightDown]=0;
	else 
		self.canAchieveTiles[EDirection.RightDown]=temp;
	end
	
	return self.canAchieveTiles;
end

function CardControl:NaCarCanMoveTo(tile)
	-- �೵��������/���Ϸ���ǰ��ֱ��ǰ�����������ѣ���Ҫ�ж���
	-- ��������/���Ϸ���ֱ��ǰ�����������ѣ���ÿ��������������ĸ������canAchieveTiles������
	self.canAchieveTiles = {};
	local temp = tile
	-- LeftUp
	self.canAchieveTiles[EDirection.LeftUp] = 0
	-- Left
	self.canAchieveTiles[EDirection.Left] = 0
	-- LeftDown
	while(temp.LeftDown~=nil and temp.LeftDown.isDestroyed==false)
	do
		temp = temp.LeftDown;
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.LeftDown]=0;
	else 
		self.canAchieveTiles[EDirection.LeftDown]=temp;
	end

	temp=tile
	-- RightUp
	while(temp.RightUp~=nil and temp.RightUp.isDestroyed==false)
	do
		temp = temp.RightUp;
	end
	if temp==tile then
		self.canAchieveTiles[EDirection.RightUp]=0;
	else 
		self.canAchieveTiles[EDirection.RightUp]=temp;
	end
	-- Right
	self.canAchieveTiles[EDirection.Right] = 0
	-- RightDown
	self.canAchieveTiles[EDirection.RightDown] = 0
	
	return self.canAchieveTiles;
end

-- 没问题
function CardControl:KnightCanMoveTo(tile)
	-- ��ʿ���ƶ����ٽ��ĸ��ӣ�Ѹ���ж���/��Ҫ�ж���
	-- �ƶ����ٽ���û���˵ĸ���
	self.canAchieveTiles = {};

	-- LeftUp
	if(tile.LeftUp~=nil and tile.LeftUp.isDestroyed==false and tile.LeftUp:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.LeftUp]=tile.LeftUp
	else
		self.canAchieveTiles[EDirection.LeftUp]=0
	end

	-- Left
	if(tile.Left~=nil and tile.Left.isDestroyed==false and tile.Left:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.Left]=tile.Left
	else
		self.canAchieveTiles[EDirection.Left]=0
	end

	-- LeftDown
	if(tile.LeftDown~=nil and tile.LeftDown.isDestroyed==false and tile.LeftDown:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.LeftDown]=tile.LeftDown
	else
		self.canAchieveTiles[EDirection.LeftDown]=0
	end

	-- RightUp
	if(tile.RightUp~=nil and tile.RightUp.isDestroyed==false and tile.RightUp:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.RightUp]=tile.RightUp
	else
		self.canAchieveTiles[EDirection.RightUp]=0
	end

	-- Right
	if(tile.Right~=nil and tile.Right.isDestroyed==false and tile.Right:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.Right]=tile.Right
	else
		self.canAchieveTiles[EDirection.Right]=0
	end

	-- RightDown
	if(tile.RightDown~=nil and tile.RightDown.isDestroyed==false and tile.RightDown:GetPlayerOnThisTile()==nil) then
		self.canAchieveTiles[EDirection.RightDown]=tile.RightDown
	else
		self.canAchieveTiles[EDirection.RightDown]=0
	end

	return self.canAchieveTiles;
end

return CardControl